var stompClient = null;
function setConnected(connected) {
	if (connected) {
		$("#conversation").show();
	} else {
		$("#conversation").hide();
	}
	$("#greetings").html("");
}

function connect() {
	var socket = new SockJS('/nginx-log-server');
	stompClient = Stomp.over(socket);
	stompClient.connect({}, function(frame) {
		setConnected(true);
		console.log('Connected: ' + frame);
		stompClient.subscribe('/log/access/realtime', function(realtimeData) {
			showChart(JSON.parse(realtimeData.body));
		});
		stompClient.subscribe('/log/access/warn', function(warnData) {
			showWarnData(JSON.parse(warnData.body));
		});
	});
}

function showChart(realdata) {
	if(realDatas.length>600){
		realDatas.shift();
	}
	realDatas.push(transData(realdata));
	myChart.setOption({
		series : [ {
			data : realDatas
		} ]
	});
}
function showWarnData(warndata) {
	if($("#warnTable tr").length>=10){
		$("#warnTable tr:last").remove();
	}
	$("#warnTable tbody").prepend("<tr><td >"+warndata.datetime+"</td><td >"+warndata.number+"</td></tr>");
}
function transData(data) {
	return {
		name : data.datetime,
		value : [ data.datetime, data.number ]
	}
}

$(function() {
	connect();
});